//window to show details of individual user account. Reached by clicking on a row in users.js
var win = Titanium.UI.currentWindow;

var data = [];
//user record brought over from previous window
var userData = win.userData;

var TheTable = Titanium.UI.createTableView({});
var nameRow = Titanium.UI.createTableViewRow({ height: 70 });

//create the 1st row w/ name
var photo = Titanium.UI.createImageView({
url: 'images/user_32.png',
    width: 32,
    height: 32,
    left: 1,
    top: 20
});

var displayName = Titanium.UI.createLabel({
text: userData.DisplayName,
    font: { fontSize: 36, fontWeight: 'bold' },
    width: 'auto',
    textAlign: 'left',
    top: 8,
    left: 50,
    height: 48
});

nameRow.add(photo);
nameRow.add(displayName);
data.push(nameRow);

//create row for cell phone
var cellRow = Titanium.UI.createTableViewRow({ height: 60 });

var cellImage = Titanium.UI.createImageView({
    url: 'images/bubble_32.png',
    width: 32,
    height: 32,
    left: 1,
    top: 9
});

var cellLabel = Titanium.UI.createLabel({
    text: 'Call Mobile',
    font: { fontSize: 24, fontWeight: 'bold' },
    width: 'auto',
    textAlign: 'left',
    top: 4,
    left: 60,
    height: 36
});

var cell = Titanium.UI.createLabel({
text: userData.Profile.Cell,
    font: { fontSize: 14 },
    width: 'auto',
    textAlign: 'left',
    bottom: 8,
    left: 65,
    height: 16
});
cellRow.add(cellImage);
cellRow.add(cellLabel);
cellRow.add(cell);
data.push(cellRow);


//create row for telephone
var phoneRow = Titanium.UI.createTableViewRow({ height: 60 });

var phoneImage = Titanium.UI.createImageView({
    url: 'images/bubble_32.png',
    width: 32,
    height: 32,
    left: 1,
    top: 9
});

var phoneLabel = Titanium.UI.createLabel({
    text: 'Call Telephone',
    font: { fontSize: 24, fontWeight: 'bold' },
    width: 'auto',
    textAlign: 'left',
    top: 4,
    left: 60,
    height: 36
});

var phone = Titanium.UI.createLabel({
    text: userData.Profile.Telephone,
    font: { fontSize: 14 },
    width: 'auto',
    textAlign: 'left',
    bottom: 8,
    left: 65,
    height: 16
});
phoneRow.add(phoneImage);
phoneRow.add(phoneLabel);
phoneRow.add(phone);
data.push(phoneRow);

//create row for sms to mobile
var smsRow = Titanium.UI.createTableViewRow({ height: 60 });

var smsImage = Titanium.UI.createImageView({
    url: 'images/document_32.png',
    width: 32,
    height: 32,
    left: 1,
    top: 9
});

var smsLabel = Titanium.UI.createLabel({
    text: 'Text Mobile',
    font: { fontSize: 24, fontWeight: 'bold' },
    width: 'auto',
    textAlign: 'left',
    top: 4,
    left: 60,
    height: 36
});

var sms = Titanium.UI.createLabel({
    text: userData.Profile.Cell,
    font: { fontSize: 14 },
    width: 'auto',
    textAlign: 'left',
    bottom: 8,
    left: 65,
    height: 16
});
smsRow.add(smsImage);
smsRow.add(smsLabel);
smsRow.add(sms);
data.push(smsRow);

//create row for emailing out
var emailRow = Titanium.UI.createTableViewRow({ height: 60 });

var emailImage = Titanium.UI.createImageView({
    url: 'images/letter_32.png',
    width: 32,
    height: 32,
    left: 1,
    top: 9
});

var emailLabel = Titanium.UI.createLabel({
    text: 'Send Email',
    font: { fontSize: 24, fontWeight: 'bold' },
    width: 'auto',
    textAlign: 'left',
    top: 4,
    left: 60,
    height: 36
});

var email = Titanium.UI.createLabel({
    text: userData.Email,
    font: { fontSize: 14 },
    width: 'auto',
    textAlign: 'left',
    bottom: 8,
    left: 65,
    height: 18
});
emailRow.add(emailImage);
emailRow.add(emailLabel);
emailRow.add(email);
data.push(emailRow);


TheTable.setData(data);

win.add(TheTable);

TheTable.addEventListener('click', function(e) {

    var n = e.index;
    //there's probably a better way to determine who sent the event than by row index
    switch(n)
    {
    case 1:
        //open phone dialer w/ cell number
        Ti.Platform.openURL('tel:' + userData.Profile.Cell);
        break;
    case 2:
        //open phone dialer w/ telephone number
        Ti.Platform.openURL('tel:' + userData.Profile.Telephone);
        break;
    case 3:
        //open sms text message w/ cell number
        Ti.Platform.openURL('sms:' + userData.Profile.Cell);
        break;
    case 4:
        //send an email using createemaildialog
        var emailDialog = Titanium.UI.createEmailDialog()
        emailDialog.toRecipients = [userData.Email];
        emailDialog.open();
        break;
    default:
    }

});
